package com.yunhe.mybatis.service.impl;

import com.yunhe.mybatis.common.utils.SqlSessionManager;
import com.yunhe.mybatis.dao.deparment.DeparmentDao;
import com.yunhe.mybatis.dao.user.UserDao;
import com.yunhe.mybatis.dto.Pageable;
import com.yunhe.mybatis.entity.Deparment;
import com.yunhe.mybatis.entity.User;
import com.yunhe.mybatis.service.DeparmentService;

import java.util.List;

public class DeparmentServiceImpl implements DeparmentService {

    @Override
    public void save(Deparment deparment) {
        DeparmentDao deparmentDao= SqlSessionManager.getSqlSession().getMapper(DeparmentDao.class);
        deparmentDao.save(deparment);
    }

    @Override
    public void update(Deparment deparment) {
        DeparmentDao deparmentDao= SqlSessionManager.getSqlSession().getMapper(DeparmentDao.class);
        deparmentDao.update(deparment);
    }

    @Override
    public void remove(Integer id) {
        DeparmentDao deparmentDao= SqlSessionManager.getSqlSession().getMapper(DeparmentDao.class);
        deparmentDao.remove(id);
    }

    @Override
    public void removebath(Integer[] ids) {
        DeparmentDao deparmentDao= SqlSessionManager.getSqlSession().getMapper(DeparmentDao.class);
        deparmentDao.removebath(ids);
    }

    @Override
    public Deparment find(Integer id) {
        DeparmentDao deparmentDao= SqlSessionManager.getSqlSession().getMapper(DeparmentDao.class);
        return deparmentDao.find(id);
    }

    @Override
    public Deparment findByName(String name) {
        DeparmentDao deparmentDao= SqlSessionManager.getSqlSession().getMapper(DeparmentDao.class);
        return deparmentDao.findByName(name);
    }

    @Override
    public List<Deparment> findAll() {
        DeparmentDao deparmentDao= SqlSessionManager.getSqlSession().getMapper(DeparmentDao.class);
        return deparmentDao.findAll();
    }

    @Override
    public Pageable findForPage(int page, int size, String name) {
        DeparmentDao deparmentDao= SqlSessionManager.getSqlSession().getMapper(DeparmentDao.class);
        Pageable pageable = new Pageable();
        pageable.setCurrent(page);
        pageable.setSize(size);
        int count = deparmentDao.count();
        pageable.setCount(count);
        if(count > 0 ){
            name = name != null ? "%" + name +"%" : name;
            List<User> list = deparmentDao.findList((page - 1) * size, size,name);
            pageable.setRows(list);

            int pageCount = count/size;
            pageCount = count % size == 0 ? pageCount : pageCount + 1;
            pageable.setPageCount(pageCount);
        }
        return pageable;
    }
}
